var js = jQuery.noConflict();
$(function () {

    //加载头部
    $("#tou").load("Header.html",function () {
        $(".leftNav").attr("class","leftNav none");

        $(".nav").hover(function () {
            $(".leftNav").attr("class","leftNav");
        },function () {
            $(".leftNav").attr("class","leftNav none");
        })
    });

//加载购物车详情页购物车商品的列表
    productList();

    //加载收件人信息
    userInformation();

    //加载用户地址信息
    userAddress();

    //新增用户地址
    addAddress();

    //订单提交
    submitOrder()
});

//加载购物车详情页购物车商品列表
function productList() {
    $.ajax({
        url:"easybuy/carprolist",
        success:function (param){
            var products = param.products;
            var prices = param.prices;
            var i = 0;
            $.each(products,function (index,product){
                $.ajax({
                    url:"easybuy/product",
                    data:{
                        productId:product.id
                    },
                    success:function (sqlProduct) {
                        $(".car_tab").eq(0).children().find("tr").eq(0).after("" +
                            "               <tr>\n" +
                            "                <td>\n" +
                            "                    <div class=\"c_s_img\"><img src=\"http://47.104.137.246:9999/files/"+product.filename+"\" width=\"73\" height=\"73\" /></div><span>\n" +
                            "                    <div style='text-align: center;line-height: 75px'><a >"  +product.name+"</a></div>"+
                            "                </td>\n" +
                            "                <td align=\"center\">"+product.description+"</td>\n" +
                            "                <td align=\"center\">"+product.carcount+"</td>\n" +
                            "                <td align=\"center\" style=\"color:#ff4e00;\">￥"+sqlProduct.price+"</td>\n" +
                            "                <td align=\"center\">26R</td>\n" +
                            "              </tr>");

                        if (index%2==1){
                            $(".car_tab").eq(0).children().children("tr").eq(index+1).attr("class","car_tr");
                        }


                        if (index==products.length-1){
                            $(".car_tab").eq(0).children().last().append("" +
                                "               <tr>\n" +
                                "                <td colspan=\"5\" align=\"right\" style=\"font-family:'Microsoft YaHei';\">\n" +
                                "                    商品总价：￥"+prices+" ； 返还积分 56R  \n" +
                                "                </td>\n" +
                                "              </tr>");
                            $(".yuanjia").text(prices);

                            //页面最下边的总金额
                            if (prices>1000){
                                $(".priceSum").text(prices);
                            }else {

                                prices = prices + 15;
                                $(".priceSum").text(prices);
                                $(".delivery").append("+ 配送费用: <font color=\"#ff4e00\">￥15.00</font>");
                            }
                        }
                    }
                })
            });
        }
    });
}

//加载收件人信息
function userInformation() {
    $.ajax({
        url: "easybuy/userinformation",
        success:function (user) {
            $(".loginName").text(user.loginname);
            $(".userName").text(user.username);
            $(".mobile").text(user.mobile);
            $(".email").text(user.email);
        }
    })
}

//加载收件地址
function userAddress() {
    $.ajax({
        url:"easybuy/addresslist",
        success:function (userAddressList) {
            $(".user_address").last().empty();
            $(".user_address").last().append("" +
                "               <tr>\n" +
                "                <td class=\"car_th\" width=\"150\"></td>\n" +
                "                <td class=\"car_th\" width=\"600\">地址</td>\n" +
                "                <td class=\"car_th\" width=\"260\">备注</td>\n" +
                "              </tr>")
            $.each(userAddressList,function (index,userAddress) {
                if (userAddress.isdefault==1){
                    $(".user_address").last().append("" +
                        "              <tr>\n" +
                        "              \t<td align=\"center\"><input type=\"radio\" name=\"wish\" checked=\"checked\"/></td>\n" +
                        "                <td style=\"text-align: center\"><b style=\"font-size:14px;\">"+userAddress.address+"</b></td>\n" +
                        "                <td align=\"center\">"+userAddress.remark+"</td>\n" +
                        "              </tr>");
                }else {
                    $(".user_address").last().append("" +
                        "              <tr>\n" +
                        "              \t<td align=\"center\"><input type=\"radio\" name=\"wish\" /></td>\n" +
                        "                <td style=\"text-align: center\"><b style=\"font-size:14px;\">"+userAddress.address+"</b></td>\n" +
                        "                <td align=\"center\">"+userAddress.remark+"</td>\n" +
                        "              </tr>");
                }
            });
        }
    });
}

//新增地址
function addAddress() {
    $(".addAddress").click(function () {
        $(".order_tab").show();
        $(".order_tab").prev().show();
        $("#add").click(function () {
            var address = $("[name='address']").val();
            var remark = $("[name='remark']").val();

            if (address!=null&&remark!=null&&remark!=""&&address!=""){
                $.ajax({
                    url:"easybuy/addUserAddress",
                    data:{
                        address: address,
                        remark: remark
                    },
                    success:function (data) {
                        if (data =="success"){
                            alert("新增成功");
                            userAddress();
                            $(".order_tab").find("input").val("");
                            $(".order_tab").attr("style","display:none");
                            $(".order_tab").prev().attr("style","display:none");
                        }else {
                            alert("新增失败，请联系管理员！");
                        }
                    }
                })
            }else {
                alert("信息不能为空");
            }

        });
    });
}

//订单提交
function submitOrder() {
    $(".submit").click(function () {
        var prices = $(".priceSum").text();
        var address = $("[name='wish']:checked").parent().next().text();
        if (address==null||address==""){
            alert("请先添加地址");
            $(".addAddress").click();
            $("[name='address']").focus();
        }
        else {
            $.ajax({
                url:"easybuy/submitorder",
                data: {
                    prices:prices,
                    address:address
                },
                success:function (data) {
                    if (data=="failed"){
                        alert("下单失败，请检查网络或联系管理员！");
                    }else {
                        location.href = "BuyCar_Three.html?serialNumber="+data.serialnumber+"&prices="+data.cost;
                    }
                }
            })
        }

    });
}


